<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '220px',
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    isShow: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      chart: {
        setOption: (_) => {},
      },
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler: function (val) {
        this.setOptions(val)
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = {}
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData, true)
    },
    setOptions(chartData) {
      this.chart.clear()
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          backgroundColor: 'rgba(255,255,255,0.8)', //设置背景图片 rgba格式
          color: 'black',
          borderWidth: '1', //边框宽度设置1
          // borderColor: 'gray', //设置边框颜色
          // textStyle: {
          //   color: 'black', //设置文字颜色
          // },
        },
        legend: {
          top: '0%',
          left: 'center',
        },
        series: chartData.seriesData,
      })
    },
  },
}
</script>
